package com.jsdsft.openapi.controller;

import com.alibaba.fastjson2.JSONObject;
import com.jsdsft.openapi.rest.JsdSftRestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 交易收单相关接口
 *
 * @author xm
 * @date 2025/1/14
 **/
@RestController
@RequestMapping("ss_order")
public class MergeSsOrderController {

    @Autowired
    private JsdSftRestUtil jsdSftRestUtil;
    private final static String version = "/v1";

    @Value("${jsd_sft.mc_no}")
    public String mc_no;
    @Value("${jsd_sft.pay_callback}")
    public String pay_callback;
    @Value("${jsd_sft.refund_callback}")
    public String refund_callback;
    @Value("${jsd_sft.commission_callback}")
    public String commission_callback;

    @GetMapping("merge_jsapi")
    public Object merge_jsapi() {
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("mcNo", mc_no);
        requestMap.put("orderNo", "202401230000001");
        requestMap.put("payMethod", "WXPAY");
        requestMap.put("paySource", "MINI");
        requestMap.put("remark", "测试一下");
        requestMap.put("appId", "wxfec27aa4bac89200");
        requestMap.put("openId", "oqINX7HuCImaz_FnPj0O4EO1bOTU");
//        requestMap.put("appId", "2021005110661958");
//        requestMap.put("openId", "029bRcHdf9_HFu2bnthmFcHRkQWKLZaUo93A7Fb_T2YYmA9");
        requestMap.put("payTimeLimit", "2025-03-30 00:00:00");
        requestMap.put("extraCommonParam", "测试回调参数");
        requestMap.put("notifyUrl", pay_callback);
        requestMap.put("payerIp", "");
        requestMap.put("isDiv", "Y"); // 是否需要分账：Y-需要，N-不需要，如果设置需要，支付完毕需要商户自主调用订单完结接口才能解冻资金到商户账户
        List<JSONObject> subOrders = new ArrayList<>();
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("orderNo", "202401230000001");
        jsonObject1.put("amount", "1");
        jsonObject1.put("goodsName", "旺仔牛奶一瓶");
        subOrders.add(jsonObject1);
        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("orderNo", "202401230000002");
        jsonObject2.put("amount", "2");
        jsonObject2.put("goodsName", "旺仔牛奶2瓶");
        subOrders.add(jsonObject2);
        requestMap.put("subOrders", JSONObject.toJSONString(subOrders));
        try {
            JSONObject jsonObject = jsdSftRestUtil.post(requestMap, version + "/ss_order/merge_jsapi");

            if (!"Y000".equals(jsonObject.getString("code"))) {
                // todo 这是网关非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("msg"));
            }

            if (!"S000".equals(jsonObject.getString("subCode"))) {
                // todo 这是业务非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("subMsg"));
            }

            // todo 拿到业务数据，继续业务处理即可，例如保存单号，然后返回给前端唤起支付等
            jsonObject.put("data", jsonObject.getJSONObject("data"));
            return jsonObject;
        } catch (Exception e) {
            return e.getMessage();
        }
    }

    @GetMapping("merge_h5")
    public Object merge_h5() {
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("mcNo", mc_no);
        requestMap.put("orderNo", "202401230000001");
        requestMap.put("payMethod", "ALIPAY");
        requestMap.put("remark", "测试一下");
        requestMap.put("payTimeLimit", "2025-03-30 00:00:00");
        requestMap.put("extraCommonParam", "测试回调参数");
        requestMap.put("notifyUrl", pay_callback);
        requestMap.put("returnUrl", "https://www.baidu.com");
        requestMap.put("payerIp", "");
        requestMap.put("isDiv", "Y"); // 是否需要分账：Y-需要，N-不需要，如果设置需要，支付完毕需要商户自主调用订单完结接口才能解冻资金到商户账户
        List<JSONObject> subOrders = new ArrayList<>();
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("orderNo", "202401230000001");
        jsonObject1.put("amount", "0.01");
        jsonObject1.put("goodsName", "旺仔牛奶1瓶");
        subOrders.add(jsonObject1);
        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("orderNo", "202401230000002");
        jsonObject2.put("amount", "0.02");
        jsonObject2.put("goodsName", "旺仔牛奶2瓶");
        subOrders.add(jsonObject2);
        requestMap.put("subOrders", JSONObject.toJSONString(subOrders));
        try {
            JSONObject jsonObject = jsdSftRestUtil.post(requestMap, version + "/ss_order/merge_h5");

            if (!"Y000".equals(jsonObject.getString("code"))) {
                // todo 这是网关非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("msg"));
            }

            if (!"S000".equals(jsonObject.getString("subCode"))) {
                // todo 这是业务非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("subMsg"));
            }

            // todo 拿到业务数据，继续业务处理即可，例如保存单号，然后返回给前端唤起支付等
            jsonObject.put("data", jsonObject.getJSONObject("data"));
            return jsonObject;
        } catch (Exception e) {
            return e.getMessage();
        }
    }


    @GetMapping("merge_pc")
    public Object merge_pc() {
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("mcNo", mc_no);
        requestMap.put("orderNo", "202401230000001");
        requestMap.put("payMethod", "ALIPAY");
        requestMap.put("remark", "测试一下");
        requestMap.put("payTimeLimit", "2025-03-30 00:00:00");
        requestMap.put("extraCommonParam", "测试回调参数");
        requestMap.put("notifyUrl", pay_callback);
        requestMap.put("returnUrl", "https://www.baidu.com");
        requestMap.put("payerIp", "");
        requestMap.put("isDiv", "Y"); // 是否需要分账：Y-需要，N-不需要，如果设置需要，支付完毕需要商户自主调用订单完结接口才能解冻资金到商户账户
        List<JSONObject> subOrders = new ArrayList<>();
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("orderNo", "202401230000001");
        jsonObject1.put("amount", "0.01");
        jsonObject1.put("goodsName", "旺仔牛奶1瓶");
        subOrders.add(jsonObject1);
        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("orderNo", "202401230000002");
        jsonObject2.put("amount", "0.02");
        jsonObject2.put("goodsName", "旺仔牛奶2瓶");
        subOrders.add(jsonObject2);
        requestMap.put("subOrders", JSONObject.toJSONString(subOrders));
        try {
            JSONObject jsonObject = jsdSftRestUtil.post(requestMap, version + "/ss_order/merge_pc");

            if (!"Y000".equals(jsonObject.getString("code"))) {
                // todo 这是网关非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("msg"));
            }

            if (!"S000".equals(jsonObject.getString("subCode"))) {
                // todo 这是业务非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("subMsg"));
            }

            // todo 拿到业务数据，继续业务处理即可，例如保存单号，然后返回给前端唤起支付等
            jsonObject.put("data", jsonObject.getJSONObject("data"));
            return jsonObject;
        } catch (Exception e) {
            return e.getMessage();
        }
    }

    @GetMapping("query_merge_order")
    public Object query_order() {
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("mcNo", mc_no);
        requestMap.put("orderNo", "202401230000001");
        requestMap.put("tradeNo", "MS2025032655097000001");
        try {
            JSONObject jsonObject = jsdSftRestUtil.post(requestMap, version + "/ss_order/query_merge_order");

            if (!"Y000".equals(jsonObject.getString("code"))) {
                // todo 这是网关非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("msg"));
            }

            if (!"S000".equals(jsonObject.getString("subCode"))) {
                // todo 这是业务非成功的返回，做一些逻辑处理，具体状态码可查看文档
                throw new RuntimeException(jsonObject.getString("subMsg"));
            }

            // todo 拿到业务数据，继续业务处理即可，例如保存单号，然后返回给前端唤起支付等
            jsonObject.put("data", jsonObject.getJSONObject("data"));
            return jsonObject;
        } catch (Exception e) {
            return e.getMessage();
        }
    }
}
